<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link th:href="@{/erp/assets/css/bootstrap.min.css}" rel="stylesheet"/>
    <link rel="stylesheet" th:href="@{/erp/css/style.css}"/>
    <link th:href="@{/erp/assets/css/codemirror.css}" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/erp/assets/css/ace.min.css}"/>
    <link rel="stylesheet" th:href="@{/erp/font/css/font-awesome.min.css}"/>
    <link th:href="@{/erp/assets/css/bootstrap.min.css}" rel="stylesheet"/>
    <link rel="stylesheet" th:href="@{/erp/css/style.css}"/>
    <link rel="stylesheet" th:href="@{/erp/assets/css/ace.min.css}"/>
    <link rel="stylesheet" th:href="@{/erp/assets/css/font-awesome.min.css}"/>
    <link rel="stylesheet" th:href="@{/erp/Widget/zTree/css/zTreeStyle/zTreeStyle.css}" type="text/css">
    <link th:href="@{/erp/Widget/icheck/icheck.css}" rel="stylesheet" type="text/css"/>
    <!--[if lte IE 8]>
    <link rel="stylesheet" th:href="@{/erp/assets/css/ace-ie.min.css}"/>
    <![endif]-->
    <script th:src="@{/erp/js/jquery-1.9.1.min.js}"></script>
    <script th:src="@{/erp/assets/js/bootstrap.min.js}"></script>
    <script th:src="@{/erp/assets/js/typeahead-bs2.min.js}"></script>
    <script th:src="@{/erp/assets/js/jquery.dataTables.min.js}"></script>
    <script th:src="@{/erp/assets/js/jquery.dataTables.bootstrap.js}"></script>
    <script th:src="@{/erp/assets/layer/layer.js}" type="text/javascript"></script>
    <script th:src="@{/erp/assets/laydate/laydate.js}" type="text/javascript"></script>

    <title>入库添加</title>
</head>
<body>

<!--入库添加的界面-->
<div class="add_menber" id="add_in_menber">
    <ul class=" page-content">
        <form>
            <input hidden name="handoverId" id="handoverId" th:value="${handoverId}">
            <input hidden name="oNo" id="oNo" th:value="${pByOrderId.order_id}">
            <input hidden name="wId" id="wId" th:value="${ware.id}">
            <input hidden name="wName" id="wName" th:value="${ware.wareName}">
            <input hidden name="pNum" id="pNum" th:value="${pByOrderId.number}">
            <input hidden name="pName" id="pName" th:value="${pByOrderId.p_name}">
            <input hidden name="pNo" id="pNo" th:value="${pByOrderId.p_number}">
            <div class="Operate_cont clearfix">
                <label class="form-label"><span class="c-red"></span>入库单号：</label>
                <div class="formControls ">
                    <input type="text" class="input-text" readonly id="inNo" name="ioNo">
                </div>
            </div>

            <div class="Operate_cont clearfix">
                <label class="form-label"><span class="c-red"></span>入库类型：</label>
                <div class="formControls ">
                    <select style="margin-left:10px;" disabled="disabled">
                        <option value="采购订单入库" th:selected="${inType=='采购交接单'}">采购订单入库</option>
                        <option value="销售订单入库" th:selected="${inType=='生产交接单'}">销售订单入库</option>
                    </select>
                    <input type="hidden" name="ioType" readonly="readonly" th:value="${inType}">
                </div>
            </div>

            <div class="Operate_cont clearfix">
                <label class="form-label"><span class="c-red"></span>批次：</label>
                <div class="formControls ">
                    <input type="text" class="input-text" readonly id="inBatch" name="batch">
                </div>
            </div>

            <div class="Operate_cont clearfix">
                <label class="form-label"><span class="c-red"></span>入库时间：</label>
                <div class="formControls ">
                    <input type="text" class="inline laydate-icon" value="" id="inTime" name="ioTime">
                </div>
            </div>

            <div class="Operate_cont clearfix">
                <table class="table table-striped table-bordered table-hover" id="inWare-table" style="table-layout:fixed">
                    <thead>
                    <tr>
                        <th width="70">序号</th>
                        <th width="150">产品码</th>
                        <th width="120">商品名称</th>
                        <th width="120">入库库名</th>
                        <th width="150">入库区域</th>
                        <th width="150">入库货架</th>
                        <th width="150">入库货位</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
        </form>

        <div class="layui-layer-btn">
            <a class="layui-layer-btn0" id="in_button">确定入库</a>
            <a class="layui-layer-btn1" id="cancel">取消</a>
        </div>
    </ul>
</div>
<!--日期-->
<script>
    $(function () {
        laydate.render({
            elem: '#inTime',
            type: 'date'
        });
    })
</script>
<!--入库列表-->
<script>
    $(function () {
        /*按照当前的时间生成单号*/
        $('#inNo').val(new Date().getTime());
        /*按照已有的批次生成最新的批次号*/
        $.ajax({
            url: "[[@{/ioWarehouse/getBatch}]]",
            success: function (d) {
                if (d.rs) {
                    $('#inBatch').val(d.data + 1);
                } else {
                    layer.msg(d.msg,{icon:2,time:2000});
                }
            }
        })

        /*这一订单编号所有的产品列表*/
        $('#inWare-table').DataTable({
            order: [],
            paging:false,
            searching: false,
            columnDefs: [
                {"orderable": false, "targets": ["_all"]}// 制定列不参与排序
            ],
            scrollY: "150px",
            ajax: {
                url: '[[@{/ioWarehouse/getPNameAndCode}]]',
                type: 'post',
                data: function (d) {
                    var searchParams = {
                        orderId: $("#oNo").val(),
                    };
                    var data = $.param(searchParams);
                    return data;
                }
            },
            columns: [
                {
                    "data": "l_code",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).text(row + 1);
                    }
                },
                {"data": "l_code",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).append('<input hidden="hidden" value="'+cellData+'" name="iomDetails['+row+'].iomGoodsno">')
                    }},
                {"data": "p_name",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).append('<input hidden="hidden" value="'+cellData+'" name="iomDetails['+row+'].iomGoods">')
                    }},
                {
                    "data": "p_name",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).text($('#wName').val());
                        $(td).append('<input hidden="hidden" value="'+$('#wName').val()+'" name="iomDetails['+row+'].iomName">')
                    }
                },
                {
                    "data": "l_code",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).empty().append('<select class="dam_id" name="iomDetails['+row+'].iomArea" style="margin-left:10px;"><option value="">请选择货区</option></select>');
                    }
                },
                {
                    "data": "l_code",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).empty().append('<select class="allShelves" name="iomDetails['+row+'].iomShelves" style="margin-left:10px;"><option value="">请选择货架</option></select>');
                    }
                },
                {
                    "data": "l_code",'sDefaultContent': '', "createdCell": function (td, cellData, rowData, row, col) {
                        $(td).empty().append('<select class="cargo" name="iomDetails['+row+'].iomCargoloca" style="margin-left:10px;"><option value="">请选择货位</option></selectid>');
                    }
                },
            ],
            "initComplete": function( settings, json ) {
                /* 通过仓库找货区 */
                $.ajax({
                    url: "/cargo/allCargoArea?wareId="+$("#wId").val(),
                    success:function (d) {
                        if (d.rs) {
                            var data = d.data;
                            for (var i in data) {
                                $('.dam_id').append('<option value='+data[i].id+'>'+data[i].goodsName+'</option>');
                            }
                        } else {
                            layer.msg(d.msg,{icon:2,time:2000});
                        }
                    }
                })

                /*货区下拉框change事件*/
                $('.dam_id').bind("change",function (obj) {
                    obj.currentTarget.parentElement.parentElement.children[5].children[0].innerHTML="<option value=''>请选择货架</option>";
                    $.ajax({
                        url: "/cargo/allShelves",
                        data:{
                            damId:obj.currentTarget.value
                        },
                        success:function (d) {
                            if (d.rs) {
                                var data = d.data;
                                for (var i in data) {
                                    obj.currentTarget.parentElement.parentElement.children[5].children[0].innerHTML += '<option value='+data[i].id+'>'+data[i].shelvesNo+'</option>'
                                }
                            } else {
                                layer.msg(d.msg,{icon:2,time:2000});
                            }
                        }
                    })
                })

                /*货架下拉框change事件*/
                $('.allShelves').bind("change",function (obj) {
                    obj.currentTarget.parentElement.parentElement.children[6].children[0].innerHTML="<option value=''>请选择货位</option>";
                    $.ajax({
                        url: "/cargo/allCargoByShelves",
                        data:{
                            shelvesId:obj.currentTarget.value
                        },
                        success:function (d) {
                            if (d.rs) {
                                var data = d.data;
                                for (var i in data) {
                                    obj.currentTarget.parentElement.parentElement.children[6].children[0].innerHTML += '<option value='+data[i].id+'>'+data[i].cargoNo+'</option>'
                                }
                            } else {
                                layer.msg(d.msg,{icon:2,time:2000});
                            }
                        }
                    })
                })
            }
        });
    })
</script>
<!--确定入库按钮和取消的点击事件-->
<script>
    $(function () {
        /*取消*/
        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        })
        /*确定*/
        $('#in_button').click(function () {
            var num = 0;
            var str = "";
            /*输入框不能为空*/
            $("form input[type$='text']").each(function (n) {
                if ($(this).val() == "") {
                    layer.alert(str += "" + $(this).context.parentElement.parentElement.firstElementChild.textContent.split("：",1) + "不能为空！\r\n", {
                        title: '提示框',
                        icon: 0,
                    });
                    num++;
                    return false;
                }
            });
            /*选择框不能为空*/
            $("select").each(function (n) {
                if ($(this).val() == "") {
                    num++;
                    layer.alert(str += "" + $(this).context.parentElement.children[0].firstElementChild.text.split("请选择",2)[1] + "不能为空！\r\n", {
                        title: '提示框',
                        icon: 0,
                    });
                    return false;
                }
            });
            /*货位不能重复*/
            var rows = $("#inWare-table tbody").find("tr").length;
            $('#inWare-table .cargo').css("border","1px solid #D5D5D5");
            for(var i = 0;i<rows;i++){
                for(var j = i+1;j<rows;j++){
                    var one = $("select[name='iomDetails["+i+"].iomCargoloca']").val();
                    var two = $("select[name='iomDetails["+j+"].iomCargoloca']").val();
                    if(one==two){
                        num++;
                        layer.msg("货位不能相同！", {icon: 2, time: 2000});
                        $("select[name='iomDetails["+i+"].iomCargoloca']").css("border","1px solid #f00");
                        $("select[name='iomDetails["+j+"].iomCargoloca']").css("border","1px solid #f00");
                    }
                }
            }

            if (num > 0) {
                return false;
            } else {
                /*添加*/
                var data = $('form').serialize();
                $.ajax({
                    url : '/ioWarehouse/addIn',
                    type : "post",
                    data : data,
                    success : function (d) {
                        if (d.rs){
                            console.log("123");
                            $('#btn_search', parent.document).click();
                            parent.layer.alert('入库添加成功！', {
                                title: '提示框',
                                icon: 1,
                            });
                            //关闭弹出窗
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        } else {
                            parent.layer.msg(d.msg, {icon:2,time:2000});
                        }
                    }
                })
            }
        })
    })
</script>
</body>
</html>

